package com.jsgt.greekfire.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.fhx.core.base.BaseViewModel
import com.jsgt.greekfire.bean.BroadcastTransactionBean
import com.jsgt.greekfire.bean.DelegateAccountBean
import com.jsgt.greekfire.bean.EntrustBean
import com.jsgt.greekfire.bean.UndelegateAccountBean
import com.jsgt.greekfire.common.http.ResponseData
import com.jsgt.greekfire.common.http.safeLaunch
import com.jsgt.greekfire.repository.EntrustRepository
import java.math.BigDecimal
import java.math.BigInteger

class EntrustViewModel : BaseViewModel() {
    private val entrustRepository = EntrustRepository.instance
    val errorLiveData by lazy { MutableLiveData<Any>() }
    val delegateListData by lazy { MutableLiveData<List<EntrustBean>>() }
    val delegateaccountListData by lazy { MutableLiveData<DelegateAccountBean>() }
    val broadcasttransactionListData by lazy { MutableLiveData<BroadcastTransactionBean>() }
    val undelegateaccountListData by lazy { MutableLiveData<UndelegateAccountBean>() }

    /**
     * 委托列表
     */
    fun getDelegateList(
        visible: String
    ) {
        viewModelScope.safeLaunch {
            block = {
                val data = entrustRepository.getDelegateList(visible)
                if (data != null) {
                    delegateListData.value = data.delegates!!
                }
            }
            onError = {
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 委托挖矿
     * owner_address	是	string	调用者地址
    delegate_address	是	string	委托地址
    delegate_count	是	long	委托额度
    visible	否	string	账户地址是否为 Base58check 格式, 默认为 false, 使用 HEX 地址
     */
    fun delegateaccount(
        owner_address: String,
        delegate_address: String,
        delegate_count: BigInteger,
        visible: String
    ) {
        val params =
            "{\"owner_address\":\"${owner_address}\",\"delegate_address\":\"${delegate_address}\",\"delegate_count\":${delegate_count},\"visible\":\"${visible}\"}"


        viewModelScope.safeLaunch {
            block = {
                val datas = entrustRepository.delegateaccount(params)
                if (datas != null) {
                    delegateaccountListData.value = datas
                }
            }
            onError = {
//                errorLiveData.value = it.message
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 取消委托
     * owner_address	是	string	调用者地址
    delegate_address	是	string	委托地址
    visible	否	string	账户地址是否为 Base58check 格式, 默认为 false, 使用 HEX 地址
     */
    fun undelegateaccount(owner_address: String, delegate_address: String, visible: String) {
        val params =
            "{\"owner_address\":\"${owner_address}\",\"delegate_address\":\"${delegate_address}\",\"visible\":\"${visible}\"}"
        viewModelScope.safeLaunch {
            block = {
                val datas = entrustRepository.undelegateaccount(params)
                if (datas != null) {
                    undelegateaccountListData.value = datas
                }
            }
            onError = {
//                errorLiveData.value = it.message
                errorLiveData.value = it.message
            }
        }
    }

    /**
     * 广播签名后的交易
     */
    fun broadcasttransaction(params: String) {
        viewModelScope.safeLaunch {
            block = {
                val datas = entrustRepository.broadcasttransaction(params)
                if (datas != null) {
                    broadcasttransactionListData.value = datas
                }
            }
            onError = {
//                errorLiveData.value = it.message
                errorLiveData.value = it.message
            }
        }
    }
}